﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeThanhDia.MapAndUpgrade
{
    public class File_Upgrade_Star
    {
        public static bool[] ItemBought = new bool[24];
        public static int Star;
        static IsolatedStorageFile storage;
        public static void getData()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("bvtd_Upgrade_Star.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_Upgrade_Star.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0");
                    for (int count = 0; count < 24; count++)
                    {
                        writer.WriteLine("false");
                    }
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("bvtd_Upgrade_Star.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            int i = 0;
            using (StreamReader r = new StreamReader(f))
            {
                Star = Convert.ToInt32(r.ReadLine());
                do
                {
                    ItemBought[i] = Convert.ToBoolean(r.ReadLine());
                    i++;
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
        }
        public static void SaveStar()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("bvtd_Upgrade_Star.txt"))
            {
                storage.DeleteFile("bvtd_Upgrade_Star.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_Upgrade_Star.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                writer.WriteLine(Star.ToString());
                for (int count = 0; count < 24; count++)
                {
                    writer.WriteLine(ItemBought[count].ToString());
                }
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        } // lưu lại số sao
        public static void SaveItemBought(int index, bool value)
        {
            ItemBought[index] = value;
            SaveStar();
        } // lưu lại thay đổi mua vật phẩm
    }
}
